const mongoose = require('mongoose');
var express = require('express')
var resumeBox = express()

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {resumeBoxService } = require('../../service/impl/resumeBoxService/index');
const {resumeUserInfoService} = require('../../service/impl/resumeUserInfoService/index');
/* 
  let fills ={
    "key1":{
      "path":"kemuid",
    },
    "key2":{
      "path":"resumeBoxid",
   }
  }

*/


resumeBox.post('/post/resumeBox/add', async (req,res) => {
  let result
  console.log(req.body,33);

  try {
    let news  = req.body.news
    let resumeUserInfo = await resumeUserInfoService.getIns().findOne(
      {
        "userid":news.sender
      }
    )
    // 将查询结果转为普通对象
    let resumeUserInfoCopy = resumeUserInfo.toObject();

    news.resume = resumeUserInfoCopy

    result =  await resumeBoxService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  resumeBox.post('/post/resumeBox/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await resumeBoxService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  resumeBox.post('/post/resumeBox/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await resumeBoxService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  resumeBox.post('/post/resumeBox/query', async (req,res) => {
    let result
    try {
      result =  await resumeBoxService.getIns().find()
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })


  // 查询公司的收到的简历
  resumeBox.post('/post/resumeBox/queryByCompanyID', async (req,res) => {
    console.log(req.body.conditions.companyid,333);
    let result
    let fills ={
      "key1":{
        "path":"sender",
      }
    }
    try {
      result =  await resumeBoxService.getIns().find(
        {
          "companyid":req.body.conditions.companyid
        },
        null,
        fills
     
      )
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  module.exports= resumeBox